<template>
	<view class="page">
		<flowbefore :beflows="beflows" :status="status" :isBtnClick="isBtnClicks" @save="save" @submit="submit" :statusType="statusType">
			<template>
				<view class="padding-bottom-100">
					<form class="flowform">
						<view class="formlist inputtext" v-if="$formOperates.read('flowTitle',formOperatesData)">
							<view class="formgroup border-bottom-E3E3E5">
								<view class="labletitle"><label>流程标题</label><text class="required">*</text></view>
								<view class="controlvalue">
									<input type="text" v-model="leaveApply.flowTitle" name="flowTitle" :disabled="judge('flowTitle')" />
								</view>
							</view>
						</view>
						<view class="formlist inputtext" v-if="$formOperates.read('billNo',formOperatesData)">
							<view class="formgroup border-bottom-E3E3E5">
								<view class="labletitle"><label>单号编号</label></view>
								<view class="controlvalue">
									<input type="text" v-model="leaveApply.billNo" name="billNo" :disabled="judge('billNo')" />
								</view>
							</view>
						</view>
						<view class="formlist inputtext" @tap="flowUrgent" v-if="$formOperates.read('flowUrgent',formOperatesData)">
							<view class="formgroup border-bottom-E3E3E5">
								<view class="labletitle"><label>紧急程度</label><text class="required">*</text></view>
								<view class="controlvalue">
									<input type="text" v-model="leaveApply.flowUrgent" name="flowUrgent" placeholder="选择"/>
								</view>
								<view class="advanceicon iconfont icon-xiangyou"></view>
							</view>
						</view>
						<view class="formlist inputtext" v-if="$formOperates.read('applyUser',formOperatesData)">
							<view class="formgroup border-bottom-E3E3E5">
								<view class="labletitle"><label>申请人员</label></view>
								<view class="controlvalue">
									<input type="text" v-model="leaveApply.applyUser" name="applyUser" :disabled="judge('applyUser')" />
								</view>
							</view>
						</view>
						<view class="formlist inputtext" v-if="$formOperates.read('applyDept',formOperatesData)">
							<view class="formgroup border-bottom-E3E3E5">
								<view class="labletitle"><label>所属部门</label></view>
								<view class="controlvalue">
									<input type="text" v-model="leaveApply.applyDept" name="applyDept" :disabled="judge('applyDept')" />
								</view>
							</view>
						</view>
						<view class="formlist inputtext" v-if="$formOperates.read('applyPost',formOperatesData)">
							<view class="formgroup border-bottom-E3E3E5">
								<view class="labletitle"><label>员工职务</label></view>
								<view class="controlvalue">
									<input type="text" v-model="leaveApply.applyPost" name="applyPost" :disabled="judge('applyPost')" />
								</view>
							</view>
						</view>
						<view class="formlist inputtext" v-if="$formOperates.read('applyDate',formOperatesData)">
							<view class="formgroup">
								<view class="labletitle"><label>申请日期</label></view>
								<view class="controlvalue">
									<input type="text" v-model="leaveApply.applyDate" name="applyDate" :disabled="judge('applyDate')" />
								</view>
							</view>
						</view>
						<view class="formlist inputtext margin-top-20" @tap="leaveType" v-if="$formOperates.read('leaveType',formOperatesData)">
							<view class="formgroup border-bottom-E3E3E5">
								<view class="labletitle"><label>请假类别</label><text class="required">*</text></view>
								<view class="controlvalue">
									<input type="text" v-model="leaveApply.leaveType" name="leaveType" placeholder="选择"/>
								</view>
								<view class="advanceicon iconfont icon-xiangyou"></view>
							</view>
						</view>
						<view class="formlist inputtext" @tap="startDate" v-if="$formOperates.read('leaveStartTime',formOperatesData)">
							<view class="formgroup border-bottom-E3E3E5">
								<view class="labletitle"><label>起始时间</label><text class="required">*</text></view>
								<view class="controlvalue">
									<input type="text" v-model="leaveApply.leaveStartTime" name="leaveStartTime" placeholder="选择"/>
								</view>
								<view class="advanceicon iconfont icon-xiangyou"></view>
							</view>
						</view>
						<view class="formlist inputtext" @tap="endDate" v-if="$formOperates.read('leaveEndTime',formOperatesData)">
							<view class="formgroup border-bottom-E3E3E5">
								<view class="labletitle"><label>结束时间</label><text class="required">*</text></view>
								<view class="controlvalue">
									<input type="text" v-model="leaveApply.leaveEndTime" name="leaveEndTime" placeholder="选择"/>
								</view>
								<view class="advanceicon iconfont icon-xiangyou"></view>
							</view>
						</view>
						<view class="formlist inputtext" v-if="$formOperates.read('leaveDayCount',formOperatesData)">
							<view class="formgroup border-bottom-E3E3E5">
								<view class="labletitle"><label>请假天数</label><text class="required">*</text></view>
								<view class="controlvalue">
									<input type="number" v-model="leaveApply.leaveDayCount" name="leaveDayCount" maxlength="2" placeholder="输入天数"
									 placeholder-style="color:#999" :disabled="judge('leaveDayCount')" />
								</view>
							</view>
						</view>
						<view class="formlist inputtext" v-if="$formOperates.read('leaveHour',formOperatesData)">
							<view class="formgroup border-bottom-E3E3E5">
								<view class="labletitle"><label>请假小时</label><text class="required">*</text></view>
								<view class="controlvalue">
									<input type="number" v-model="leaveApply.leaveHour" name="leaveHour" maxlength="3" placeholder="输入小时"
									 placeholder-style="color:#999" :disabled="judge('leaveHour')" />
								</view>
							</view>
						</view>
						<view class="formlist" v-if="$formOperates.read('leaveReason',formOperatesData)">
							<view class="formgroup  padding-17">
								<view class="labletitletar"><label>请假原因</label><text class="required">*</text></view>
								<view class="controlvalue  uni-textarea">
									<textarea v-model="leaveApply.leaveReason" name="leaveReason" maxlength="500" :disabled="judge('leaveReason')"
									 placeholder="输入原因" />
									</view>
							</view>
						</view>
						<JNPFUpload v-model="fileList" :fileSize='2' :limit='9'></JNPFUpload>
					</form>
					<view class="height-100"></view>
				</view>
			</template>
		</flowbefore>
		<w-picker :mode="mode" :pickerList="pickerList" ref="picker" :isSearchShow="isSearchShow"></w-picker>
	</view>
</template>

<script>
	import jnpf from '@/common/js/jnpf.js'
	import helper from '@/common/js/helper.js'
	import graceChecker from '@/common/js/graceChecker.js'
	import wPicker from "@/components/w-picker/w-picker.vue"
	import flowbefore from '@/components/work/flow-before.vue'
	let formRule=[
			{name:"flowTitle",checkType:"notnull",errorMsg:"流程标题必填"},
			{name:"flowUrgent", checkType : "notnull", checkRule:"",  errorMsg:"选择紧急程度"},
			{name:"leaveType", checkType : "notnull", errorMsg:"选择请假类别"},
			{name:"leaveStartTime", checkType : "notnull",  errorMsg:"选择起始时间"},
			{name:"leaveEndTime", checkType : "notnull",   errorMsg:"选择结束时间"},
			{name:"leaveDayCount", checkType : "int", checkRule:"0,3",  errorMsg:"天数应为1~3个字符"},
			{name:"leaveReason",checkType:"notnull",errorMsg:"输入请假原因"},
			{name:"leaveHour",checkType:"notnull",errorMsg:"输入请假小时"},
	];
	export default {
		components: {
			wPicker,
			flowbefore,
		},
		data() {
			return {
				mode:'',//底部弹窗分类
				isSearchShow:false,
				disabled:false,//启用
				status:'',//流程状态
				beflows:{
					operstate:"",
					processId:''
				},
				leaveApply:{
					billNo:'',
					applyDate:helper.toDate(new Date, 'yyyy-MM-dd'),
					flowUrgent:'',
					flowTitle:jnpf.userInfo().realName+'的请假申请',
					applyUser:jnpf.userInfo().realName+"/"+jnpf.userInfo().account,
					applyDept:jnpf.userInfo().departmentName,
					applyPost:jnpf.userInfo().positionName,
					leaveType:'',
					leaveStartTime:'',
					leaveEndTime:'',
					leaveReason:'',
					fileJson:'',
					leaveHour:''
				},
				fileList:[],
				fileJson:[],
				isBtnClicks:false,
				pickerList:[],
				urgentList:[{
						label: "普通",
						value:"1"
					},
					{
						label: "重要",
						value:"2"
					},
					{
						label: "紧急",
						value:"3"
					}],
				leaveTypeList:[{
						label: "事假",
						value:"0"
					},
					{
						label: "病假",
						value:"1"
					},
					{
						label: "婚假",
						value:"2"
					},
					{
						label: "产假",
						value:"3"
					},
					{
						label: "丧假",
						value:"4"
					},
					{
						label: "年假",
						value:"5"
					},
					{
						label: "调休",
						value:"6"
					},
					{
						label: "其他",
						value:"7"
					}
				],
				statusType:'',
				formOperatesData:[],
				F_Status: 0
			};
		},
		
		onLoad(e) {
			/* 工作区进入状态 */
			let statusType = uni.getStorageSync("type")
			this.statusType = statusType
			let F_Status = uni.getStorageSync('F_Status')
			if(F_Status == 1 || F_Status == 2){
				this.disabled = true;
			}
			/* 工作区进入状态 End*/
			let flowInfo= JSON.parse(e.flowInfo);
			this.beflows.operstate=flowInfo.operstate;
			if(flowInfo.operstate =='add'){
				this.leaveApply.flowId=flowInfo.id;
				this.status = 0;
				uni.setStorageSync('F_Status', 0);
				this.F_Status = Number(uni.getStorageSync('F_Status'))
				this.leaveApply.flowUrgent=helper.getSpinnerNew(this.urgentList,'1');
				helper.ajax({
					url: '/api/system/Base/BillRule/BillNumber/WF_LeaveApplyNo',
					success: res => {
						this.leaveApply.billNo=res.data.data;
					}
				});
				this.getOperatesData(flowInfo.id)
			}
			if(flowInfo.operstate=='info'){
				uni.setStorageSync('F_Status', F_Status);
				this.F_Status = Number(uni.getStorageSync('F_Status'))
				this.disabled=flowInfo.disabled;
				this.beflows.processId=flowInfo.id;
				this.status=flowInfo.status;
				helper.ajax({
					url: '/api/workflow/Form/LeaveApply/' + flowInfo.id,
					success: res => {
						let _leaveApply=res.data.data;
						this.leaveApply = _leaveApply;
						this.leaveApply.flowUrgent=helper.getSpinnerNew(this.urgentList,_leaveApply.flowUrgent);
						this.leaveApply.applyDate=helper.toDate(_leaveApply.applyDate, "yyyy-MM-dd");
						this.leaveApply.leaveStartTime=helper.toDate(_leaveApply.leaveStartTime, "yyyy-MM-dd HH:mm");
						this.leaveApply.leaveEndTime=helper.toDate(_leaveApply.leaveEndTime, "yyyy-MM-dd HH:mm");
						if (_leaveApply.fileJson!="") {
							this.fileList=JSON.parse(_leaveApply.fileJson);
						}
					}
				});
				if(this.status == 0 || this.status == 3){
					this.getOperatesData(flowInfo.flowId)
				}else{
					this.$eventHub.$off('formOperates');
					this.$eventHub.$on('formOperates', (data) => {
						this.formOperatesData = data;
						console.log(this.formOperatesData)
						uni.setStorageSync('formOperatesData', data)
					})
				}
			}
		},
		methods: {
			getOperatesData(id){
				helper.ajax({
					url: '/api/workflow/Engine/FlowEngine/'+id,
					success: res => {
						let kk = JSON.parse(res.data.data.flowTemplateJson);
						this.formOperatesData = [];
						this.formOperatesData = kk.properties.formOperates
						uni.setStorageSync('formOperatesData',this.formOperatesData)
					}
				});
			},
			//判断表单权限可写
			judge(id) {
				return this.statusType == 'backlog' ? this.$formOperates.write(id, this.formOperatesData) : this.F_Status ==
					1 || this.F_Status == 2 ? true : this.$formOperates.write(id, this.formOperatesData)
			},
			flowUrgent(e){
				uni.hideKeyboard();//隐藏软键盘  
				let isWrite = this.judge('flowUrgent')
				if(!isWrite){
					this.pickerList = this.urgentList;
					this.mode="selector";
					this.$refs.picker.show();
					this.$eventHub.$off('popupmodality');
					this.$eventHub.$on('popupmodality', (data) => {
						this.leaveApply.flowUrgent = data.result;
						this.$eventHub.$off('popupmodality');
					});
				}
			},
			leaveType(e){
				uni.hideKeyboard();//隐藏软键盘  
				let isWrite = this.judge('leaveType')
				if(!isWrite){
					this.pickerList = this.leaveTypeList;
					this.mode="selector";
					this.$refs.picker.show();
					this.$eventHub.$off('popupmodality');
					this.$eventHub.$on('popupmodality', (data) => {
						this.leaveApply.leaveType  = data.result;
						this.$eventHub.$off('popupmodality');
					});
				}
			},
			startDate(e){
				uni.hideKeyboard();//隐藏软键盘  
				let isWrite = this.judge('startDate')
				if(!isWrite){
					this.mode="dateTime";
					this.$refs.picker.show();
					this.$eventHub.$off('popupmodality');
					this.$eventHub.$on('popupmodality', (data) => {
						this.leaveApply.leaveStartTime = data.result;
						this.$eventHub.$off('popupmodality');
					});
				}
			},
			endDate(e){
				uni.hideKeyboard();//隐藏软键盘  
				let isWrite = this.judge('endDate')
				if(!isWrite){
					this.mode="dateTime";
					this.$refs.picker.show();
					this.$eventHub.$off('popupmodality');
					this.$eventHub.$on('popupmodality', (data) => {
						this.leaveApply.leaveEndTime = data.result;
						this.$eventHub.$off('popupmodality');
					});
				}
			},
			submit(e){
				this.leaveApply.fileJson = JSON.stringify(this.fileList)
				let formData = this.leaveApply;
				//根据表单权限过滤必填列表
				formRule = graceChecker.filterRequiredList(formRule,this.formOperatesData)
				if (!graceChecker.check(formData,formRule)) {
					helper.msg(graceChecker.error);
					return;
				}
				this.isBtnClicks=true;
				formData.flowUrgent=helper.setSpinnerNew(this.urgentList,formData.flowUrgent);
				formData.status = 0;
				let method = "POST";
				let url = "/api/workflow/Form/LeaveApply";
				if(formData.id){
					method = "PUT";
					url=url+"/"+formData.id
				}
				formData.applyDate = Date.parse(formData.applyDate);
				formData.leaveStartTime = Date.parse(formData.leaveStartTime);
				formData.leaveEndTime = Date.parse(formData.leaveEndTime);
				helper.ajax({
					url: url,
					method:method,
					contentType: "application/json",
					data:formData,
					success: res => {
						helper.msg(res.data.msg, 'success');
						if (this.beflows.operstate=='add') {
							uni.redirectTo({
								url: '../../operate/started/started',
								success: res => {},
								fail: () => {},
								complete: () => {}
							});
						}else{
							uni.navigateBack();
						}
						this.isBtnClicks=false
					}
				});
			},
			
			save(e){
				this.leaveApply.fileJson = JSON.stringify(this.fileList)
				let formData=this.leaveApply;
				//根据表单权限过滤必填列表
				formRule = graceChecker.filterRequiredList(formRule,this.formOperatesData)
				if (!graceChecker.check(formData,formRule)) {
					helper.msg(graceChecker.error);
					return;
				}
				this.isBtnClicks=true;
				formData.flowUrgent=helper.setSpinnerNew(this.urgentList,formData.flowUrgent);
				formData.status = 1;
				let method = "POST";
				let url = "/api/workflow/Form/LeaveApply";
				if(formData.id){
					method = "PUT";
					url=url+"/"+formData.id
				}
				formData.applyDate = Date.parse(formData.applyDate);
				formData.leaveStartTime = Date.parse(formData.leaveStartTime);
				formData.leaveEndTime = Date.parse(formData.leaveEndTime);
				helper.ajax({
					url: url,
					method:method,
					contentType: "application/json",
					data:formData,
					success: res => {
						helper.msg(res.data.msg, 'success');
						if (this.beflows.operstate=='add') {
							uni.redirectTo({
								url: '../../operate/started/started',
								success: res => {},
								fail: () => {},
								complete: () => {}
							});
						}else{
							uni.navigateBack();
						}
						this.isBtnClicks=false
					}
				});
			},
		},
	}
</script>

<style>
	.required{
		color: red;
	}
</style>
